/// <reference path="./jquery-1.8.2.min.js" />
/// <reference path="./fourkill.util.js" />

function SkyPainter() {
	this.SECTION_COUNT = 6;
	this.MAGIC_NUMBER = 180.0;
	
	this.paint = function(context) {
		var viewWidth = context.viewPort.size.w,
			skyHeight = Math.round(context.viewPort.size.h / 2.0),
			stripeWidth = Math.round(viewWidth / this.SECTION_COUNT);

		var transparentSkyBlue = "#8DD0EB",
			transparentLightBlue = "#A6D6E7";

		context.backLayer.fillStyle = transparentSkyBlue;
		context.backLayer.fillRect(0, 0, viewWidth, skyHeight);

		var rects = [], 
			b = false,
			width = 0;
		for (var x = 0; x < viewWidth; x++)
		{
			var num = x - Math.floor(context.camera.bearing * this.MAGIC_NUMBER),
				l = num % stripeWidth;
			if (l < 0) l += stripeWidth; // Reverse.
			if (l < stripeWidth / 2)
			{
				b = true;
				width++;
			}
			else
			{
				if (b)
				{
					rects.push(new Rectangle(x - width, 0, width, skyHeight));
					b = false;
					width = 0;
				}
			}
		}
		rects.push(new Rectangle(viewWidth - width, 0, width, skyHeight));

		context.backLayer.fillStyle = transparentLightBlue;
		var rect;
		for (var i=0; i < rects.length; i++)
		{
		    rect = rects[i];
			context.backLayer.fillRect(rect.x, rect.y, rect.w, rect.h);
		}
	};
}

function GroundPainter() {
	this.SECTION_LENGTH = 20.0;
	this.GROUND_COLOR_1 = "#22AA22",
	this.GROUND_COLOR_2 = "#55AA55";
	
	this.paint = function(context) {
		var totalMovement = Math.round(context.camera.totalMovement);
		if (totalMovement < 0) totalMovement += this.SECTION_LENGTH * 1000000;
		var offset = totalMovement % (this.SECTION_LENGTH);
		if (offset > 0) offset = (this.SECTION_LENGTH) - offset;
		if (offset < 0) offset = Math.abs(this.SECTION_LENGTH);

		var lineLocation = context.viewPort.size.h,
			centerLocation = lineLocation / 2,
			prevLineLocation = lineLocation,
			stripeSize = 1,
			stripeIndex = 0,
			current = 0,
			c = context.camera.h + context.camera.z,
			distance = 1.0+offset,
			d2v = context.viewPort.distanceToView;

		context.backLayer.strokeStyle = this.GROUND_COLOR_1;
		context.backLayer.fillStyle = this.GROUND_COLOR_1;
		context.backLayer.fillRect(0, centerLocation, context.viewPort.size.w, context.viewPort.size.h - centerLocation);
		
		context.backLayer.strokeStyle = this.GROUND_COLOR_2;
		context.backLayer.fillStyle = this.GROUND_COLOR_2;
		while (lineLocation > centerLocation)
		{
			prevLineLocation = lineLocation;

			current = c / distance * d2v;
			lineLocation = centerLocation + Math.round(current * context.viewPort.scale);

			if (lineLocation < centerLocation) continue;
			else if (lineLocation >= context.viewPort.size.h)
				lineLocation = context.viewPort.size.h;
			else
			{
				stripeSize = prevLineLocation - lineLocation;
				if (stripeSize == 0) break;
				if (stripeIndex % 2 == 1)
				{
					context.backLayer.fillRect(0, lineLocation, context.viewPort.size.w, stripeSize);
				}
			}
			distance += (this.SECTION_LENGTH/2);
			stripeIndex++;
		}
	};
}

//*********************************** Object Painters ************************************
var ObjectPainters = {};

function MapMarkerPainter() {
    this.paint = function (context) {

        var rect = context.gameObject.locationRelativeToCamera.objectRectangle,
            x = rect.x,
            y = rect.y,
            width = rect.w,
            height = rect.h;

        context.frontLayer.fillStyle = "#000";
        context.frontLayer.fillText(context.gameObject.name,x-width/2,y -15);
        context.frontLayer.fillStyle = "#00FF00";
        context.frontLayer.beginPath();
        context.frontLayer.moveTo(x, y);
        context.frontLayer.lineTo(x + width, y);
        context.frontLayer.lineTo(x + width / 2, y + height);
        context.frontLayer.closePath();
        context.frontLayer.fill();
        }
}
ObjectPainters.MapMarker = new MapMarkerPainter();


function FourTriangleTreePainter() {
    this.TREE_TRIANGLES = 4;
    this.OPTIMAL_DISTANCE = 1500;
    this.paint = function (context) {

        var distance = context.gameObject.locationRelativeToCamera.distance;

        var rect = context.gameObject.locationRelativeToCamera.objectRectangle,
            x = rect.x,
            y = rect.y,
            width = rect.w,
            height = rect.h;

        if (distance < this.OPTIMAL_DISTANCE) {
            for (var i = 0; i < this.TREE_TRIANGLES; i++) {
                var j = this.TREE_TRIANGLES - i - 1;
                var l = x,
                    r = (x + width),
                    w = Math.abs(r - l),
                    w_off = w * (j * 0.10);
                l += w_off;
                r -= w_off;

                context.frontLayer.fillStyle = context.gameObject.color;
                context.frontLayer.beginPath();
                context.frontLayer.moveTo(x + width / 2, y + (i * height / this.TREE_TRIANGLES) - height / 4);
                context.frontLayer.lineTo(l, y + (i * height / this.TREE_TRIANGLES) + height / this.TREE_TRIANGLES);
                context.frontLayer.lineTo(r, y + (i * height / this.TREE_TRIANGLES) + height / this.TREE_TRIANGLES);
                context.frontLayer.closePath();
                context.frontLayer.fill();
            }
        } else {

            context.frontLayer.fillStyle = context.gameObject.color;
            context.frontLayer.beginPath();
            context.frontLayer.moveTo(x + width / 2, y);
            context.frontLayer.lineTo(x, y + height);
            context.frontLayer.lineTo(x+width, y + height);
            context.frontLayer.closePath();
            context.frontLayer.fill();

            //Point p1 = new Point(location.ObjectRectangle.X + location.ObjectRectangle.Width / 2, location.ObjectRectangle.Y);
            //Point p2 = new Point(location.ObjectRectangle.X, location.ObjectRectangle.Y + location.ObjectRectangle.Height);
            //Point p3 = new Point(location.ObjectRectangle.X + location.ObjectRectangle.Width, location.ObjectRectangle.Y + location.ObjectRectangle.Height);
        }
    };
}
ObjectPainters.FourTriangleTree = new FourTriangleTreePainter();

function MistPainter() {
    this.SECTION_LENGTH = 20.0;

    this.paint = function (context) {
        var viewWidth = context.viewPort.size.w,
            viewHeight = context.viewPort.size.h,
            mistHeight = Math.round(context.viewPort.size.h * context.gameObject.percentage);

        var min = mistHeight / 5;
        if (min < 4) min = 4;
        context.frontLayer.fillStyle = context.gameObject.color;
        for (var i = min; i < mistHeight; i += (min/2))
        {
            context.frontLayer.fillRect(0, ((viewHeight - i) / 2), viewWidth, i);
        }

    };
}
ObjectPainters.Mist = new MistPainter();